package pl.nith.battleships.managers;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import pl.nith.battleships.controllers.listeners.ActionCommandListener;
import pl.nith.battleships.controllers.listeners.MouseListener;
import pl.nith.battleships.models.Board;
import pl.nith.battleships.net.Client;
import pl.nith.battleships.net.Server;
import pl.nith.battleships.views.MainWindowView;

public class Manager
{
	private static Server					server				= null;
	private static Client					client				= null;

	private static MouseListener			mouseListener		= null;
	private static ActionCommandListener	actionListener		= null;

	private static GameStateManager			gameStateManager	= null;
	
	private static Board					board				= null;

	private static MainWindowView			mainWindowView		= null;
	
	private static Properties				configFile		 	= null;

	public static void initialize()
	{
		if (mouseListener == null) mouseListener = new MouseListener();
		if (actionListener == null) actionListener = new ActionCommandListener();

		if (gameStateManager == null)
			gameStateManager = new GameStateManager();

		if (mainWindowView == null) mainWindowView = new MainWindowView();
		
		if (board == null) board = new Board();
		
		if (server == null) server = new Server();
		if (client == null) client = new Client();
		
		if (configFile == null)
		{
			configFile = new Properties();
			
			try
			{
				configFile.load(ClassLoader.getSystemClassLoader().getResourceAsStream("config.properties"));
			}
			catch (FileNotFoundException e)
			{
				System.err.println(e.getMessage());
			}
			catch (IOException e)
			{
				System.err.println(e.getMessage());
			}
		}
	}

	public static void initServer()
	{
		if (server != null)
		{
			server.start();
			if (!server.isServerRunning) server.isServerRunning = true;
		}
	}

	public static void initClient()
	{
		if (client != null)
		{
			client.start();
			if (!client.isClientConnected) client.isClientConnected = true;
		}
	}

	public static Server getServer()
	{
		return server;
	}

	public static Client getClient()
	{
		return client;
	}

	public static MouseListener getMouseListener()
	{
		return mouseListener;
	}
	
	public static ActionCommandListener getActionListener()
	{
		return actionListener;
	}

	public static GameStateManager getGMS()
	{
		return gameStateManager;
	}

	public static MainWindowView getMWV()
	{
		return mainWindowView;
	}
	
	public static Board getBoard()
	{
		return board;
	}
	
	public static Properties getConfigFile()
	{
		return configFile;
	}
}
